<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Admin\Renderable\ShopGoodsRender;
use App\Admin\Renderable\TagRender;
use App\Models\Posts\WxTag;
use App\Models\Shop\WxShopGoods;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Dcat\Admin\Layout\Content;
use function PHPUnit\Framework\isNull;

trait formPageFindTrait{
    public function index_pagefind(Content $content)
    {
        return $content->title('发现页')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_pagefind()->render()}</div>
HTML
            );
    }

    protected function form_pagefind(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('圈子发现页', function (Form $form){
            $form->multipleSelectTable('app_find_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formPageFindTrait:app_find_header_icon')
                ->help('添加两个，分别是左右侧，小程序不显示右侧， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_find_header_icon', '', true));


            $form->multipleSelectTable('app_find_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formPageFindTrait:app_find_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_find_banner', '',true));

            $form->divider();

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block"><img src="https://img.mini.minisns.cn/images/page/circle_tip.png" style="width: 250px;"></div>';
            }, '');


            $form->table('app_find_tab','tab自定义', function (NestedForm $table) {
                $table->text('name', '名字')->required();
                $table->radio('follow_status', '关注状态')->options([0=>'全部', 1 => '已关注', 2 => '未关注'])->default(0);
//            $table->multipleSelect('tags', '话题')->options(function ($id) {
//                if(!is_array($id) && $id) {
//                    $tag = WxTag::find($id);
//                    if (!_empty_($tag)) {
//                        return [$tag->id => $tag->tags_name];
//                    }
//                }else{
//                    return null;
//                }
//            })->ajax('select/tags');
                $table->multipleSelectTable('tags', '话题')
                    ->title('话题库')
                    ->max(10)
                    ->from(TagRender::make())
                    ->model(WxTag::class, 'id', 'tags_name', 'app_find_tab:selectTable:formPageFindTrait:tags');
                $table->select('orderBy', '排序依据')->options(['created_at'=>'建圈时间', 'last_publish_at'=> '最后发表时间', 'last_enter_at'=>'最后访问时间', 'follow_at'=>'关注时间', 'total_post_count' =>'笔记总数量', 'total_comment_count'=>'圈子笔记的评论总数量', 'count_circle_follow'=>'圈子关注总数', 'count_pay_content'=>'圈子付费笔记总数'])->default('created_at');
                $table->radio('order', '排序方向')->options(['desc'=>'倒排', 'asc'=>'正排'])->default('desc');
                $table->switch('master', '当前用户创建')->default(0);
            })->default(Settings::get('app_find_tab', '', true));
        });

        $form->tab('商品发现页', function (Form $form){
            $form->multipleSelectTable('app_find2_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formPageFindTrait:app_find2_header_icon')
                ->help('添加两个，分别是左右侧，小程序不显示右侧， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_find2_header_icon', '', true));


            $form->multipleSelectTable('app_find2_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formPageFindTrait:app_find2_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_find2_banner', '',true));

            $form->divider();

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block"><img src="https://img.mini.minisns.cn/images/video/goodsfind.jpg" style="width: 250px;"></div>';
            }, '');

            $form->table('app_find2_tab','tab自定义', function (NestedForm $table) {
                $table->text('name', '名字')->required();

                $table->multipleSelectTable('ids', '商品范围')
                    ->title('商品库')
                    ->max(1000)
                    ->from(ShopGoodsRender::make())
                    ->model(WxShopGoods::class, 'id', 'name', 'app_find2_tab:selectTable:formPageFindTrait:ids')->help('留空代表所有商品');

                $table->multipleSelect('types', '商品类型')->saveAsString()->options(FieldUtils::getShopGoodTypes());

                $table->radio('follow_status', '关注店铺')->options([0=>'全部', 1 => '已关注', 2 => '未关注'])->default(0);

                $table->select('orderBy', '排序依据')->options(['created_at'=>'商品发布时间', 'order'=>'商品的自定义排序', 'last_enter_at'=>'最后访问时间', 'follow_shop_at'=>'关注店铺时间', 'buys' =>'商品购买人数'])->default('created_at');
                $table->radio('order', '排序方向')->options(['desc'=>'倒排', 'asc'=>'正排'])->default('desc');
                $table->switch('master', '商户为自己')->default(0);

            })->default(Settings::get('app_find2_tab', '', true));
        });


        $form->tab('图标发现页', function (Form $form){
            $form->multipleSelectTable('app_find9_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formPageFindTrait:app_find9_header_icon')
                ->help('添加两个，分别是左右侧，小程序不显示右侧， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_find9_header_icon', '', true));


            $form->divider();
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block"><a href="https://img.mini.minisns.cn/images/video/miaobian.png" target="_blank"><img src="https://img.mini.minisns.cn/images/video/miaobian.png" style="width: 250px;"></a></div>';
            }, '');

            $form->table('app_find9_icons','格子菜单', function (NestedForm $table) {
                $table->radio('type')->options([1=>'图标', 2=>'分割线'])->default(1);
                $table->text('name', '文本')->default('文字');
                $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/images/nav/nav.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->default('https://img.mini.minisns.cn/images/nav/nav.png')->uniqueName()->url('files/uploads')->autoUpload();
                $table->radio('target_type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
                $table->text('target_id', '跳转id')->help('怎么填请看<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
                $table->radio('border_type', '描边')->options([1=>'上中描边', 4=>'中描边', 2=>'上下描边', 3=>'下描边'])->default(2);
                $table->number('height', '分割线高度')->help('单位rpx')->default(20);
            })->default(Settings::get('app_find9_icons', '',true));
        });


        $form->action('settings/action/pagefind');
        return $form;
    }

    public function pagefind(Request $request)
    {
        SiteUtils::forget_config_data();

        $app_find_tab = AdminUtils::_table_data($request->input('app_find_tab'));

        Settings::set('app_find_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formPageFindTrait:app_find_header_icon', $request->input('app_find_header_icon') ), true, true);
        Settings::set('app_find_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formPageFindTrait:app_find_banner', $request->input('app_find_banner')), true, true);
        Settings::set('app_find_tab', $app_find_tab, true, true);

        $app_find2_tab = AdminUtils::_table_data($request->input('app_find2_tab'));
        Settings::set('app_find9_icons', AdminUtils::_table_data($request->input('app_find9_icons')), true, true);
        Settings::set('app_find9_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formPageFindTrait:app_find9_header_icon', $request->input('app_find9_header_icon') ), true, true);

        Settings::set('app_find2_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formPageFindTrait:app_find2_header_icon', $request->input('app_find2_header_icon') ), true, true);
        Settings::set('app_find2_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formPageFindTrait:app_find2_banner', $request->input('app_find2_banner')), true, true);
        Settings::set('app_find2_tab', $app_find2_tab, true, true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }
}
